#include "stdafx.h"
#include "Rectangle.h"
#include<algorithm>

CRectangle::CRectangle()
{
}


CRectangle::CRectangle(const CPoint& start, const CPoint &end, COLORREF color,int aPenStyle)
:CElement(start,color,aPenStyle)
{
	m_StartPoint = CPoint((std::min)(start.x, end.x), (std::min)(start.y, end.y));
	m_BottomRight = CPoint((std::max)(start.x, end.x), (std::max)(start.y, end.y));

	if ((m_BottomRight.x - m_StartPoint.x) < 2)
		m_BottomRight.x = m_StartPoint.x + 2;
	if ((m_BottomRight.y-m_StartPoint.y)<2)
		m_BottomRight.y=m_StartPoint.y+2;
	m_EnClosingRect = CRect(m_StartPoint, m_BottomRight);
	m_EnClosingRect.InflateRect(m_PenWidth, m_PenWidth);

}

CRectangle::~CRectangle()
{
}

void CRectangle::Draw(CDC* pDC,std::shared_ptr<CElement> pElement){
	CPen aPen;
	CreatePen(aPen,pElement);
	auto pOldPen = pDC->SelectObject(&aPen);
	CBrush *pOldBrush = dynamic_cast<CBrush*>(pDC->SelectStockObject(NULL_BRUSH));
	CRect crect(m_StartPoint.x, m_StartPoint.y, m_BottomRight.x, m_BottomRight.y); 
	pDC->Rectangle(&crect);
	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldPen);
}


void CRectangle::Move(const CSize &aSize)
{
	m_StartPoint += aSize;
	m_BottomRight += aSize;
	m_EnClosingRect += aSize;
}
